package com.xuan.core.service.sys.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.core.mapper.sys.SysCountMapper;
import com.xuan.core.pojo.sys.SysCount;
import com.xuan.core.service.sys.SysCountService;
import com.xuan.core.vo.sys.SysCountVo;
import org.springframework.stereotype.Service;

/**
 * 系统统计Impl
 */
@Service
public class SysCountServiceImpl extends ServiceImpl<SysCountMapper, SysCount> implements SysCountService {

    /**
     * 增加1次总访问数
     */
    @Override
    public void saveVisitsNumber() {

        SysCount sysCount = getOne(Wrappers.<SysCount>lambdaQuery().in(SysCount::getTypeName, "visits"));
        if (null != sysCount) {
            sysCount.setNumber(sysCount.getNumber() + 1);
            this.updateById(sysCount);
        }

    }

    /**
     * 增加1次总使用数
     */
    @Override
    public void saveUsageNumber() {

        SysCount sysCount = getOne(Wrappers.<SysCount>lambdaQuery().in(SysCount::getTypeName, "usage"));
        if (null != sysCount) {
            sysCount.setNumber(sysCount.getNumber() + 1);
            this.updateById(sysCount);
        }

    }

    /**
     * 查询总访问数、总使用数
     *
     * @return 总访问数、总使用数
     */
    @Override
    public SysCountVo getNumber() {

        // 初始化数据
        Long visitsNumber = 0L; // 总访问数
        Long usageNumber = 0L; // 使用问数

        // 根据类型名称查询总访问数
        SysCount visits = getOne(Wrappers.<SysCount>lambdaQuery().in(SysCount::getTypeName, "visits"));
        if (null != visits) visitsNumber = visits.getNumber();
        // 根据类型名称查询总使用数
        SysCount usage = getOne(Wrappers.<SysCount>lambdaQuery().in(SysCount::getTypeName, "usage"));
        if (null != usage) usageNumber = usage.getNumber();

        // 封装数据并返回
        SysCountVo vo = new SysCountVo();
        vo.setVisitsNumber(visitsNumber);
        vo.setUsageNumber(usageNumber);
        return vo;

    }


}
